package com.cqupt.scanning.system.admin.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqupt.scanning.system.admin.entity.Department;
import com.cqupt.scanning.system.admin.entity.Organization;
import com.cqupt.scanning.system.admin.service.DepartmentService;
import com.cqupt.scanning.system.admin.service.OrganizationService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 宋宝梁
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/admin/organization")
public class OrganizationController {

    private OrganizationService organizationService;
    private DepartmentService departmentService;

    @Autowired
    public OrganizationController(OrganizationService organizationService,DepartmentService departmentService) {
        this.organizationService = organizationService;
        this.departmentService = departmentService;
    }

    /**
     * 新增机构
     * @param organization
     * @return
     */
    @PostMapping("/insert")
    @PreAuthorize("hasAuthority('organization.insert')")
    public R insert(@RequestBody Organization organization) {
        return organizationService.insert(organization);
    }

    /**
     * 带条件分页查机构
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getAll")
    @PreAuthorize("hasAuthority('organization.getAll')")
    public R getAll(@RequestParam("query") String query,
                    @RequestParam(value = "pageNum", defaultValue = "1") Integer current,
                    @RequestParam(value = "pageSize", defaultValue = "10") Integer size) {
        Page<Organization> page = new Page<>(current, size);
        return organizationService.getAll(page, query);
    }

    /**
     * 获取当前机构、当前机构的部门、当前机构的部门的设备
     * @param id
     * @return
     */
    @GetMapping("/getDepAndDevById")
    @PreAuthorize("hasAuthority('organization.getDepAndDevById')")
    public R getDepAndDevById(Integer id) {
        return organizationService.getDepAndDevById(id);
    }

    /**
     * 通过机构id逻辑删除机构
     * @param orgId
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasAuthority('plantarScanning.delete')")
    public R removeById(@PathVariable("id") Integer orgId) {
        return organizationService.removeById(orgId);
    }

    /**
     * 修改部门（科室）与机构（医院）
     * @param dataMap
     * @return
     */
    @PutMapping("/updateOrg")
    public R update(    @RequestBody @Validated Map<String,Object> dataMap)  {
                departmentService.update(dataMap);
        return organizationService.update(dataMap);
    }
}



